#include "common_dao.h"
#include <stdio.h>
#include <string.h>
#include <mysql/mysql.h>
#include "common.h"
#include "mysql_helper.h"
#include "xml.h"

int
dao_init_index_page(char *data, int size)
{
    struct xml *root = NULL;
    if ((root = create_xml_root("root", NULL)) == NULL)
    {
        return (-1);
    }

    /* search */
    char sql[4096];
    snprintf(sql, sizeof(sql), "select * from  init_page where name like 'index%%'");
	MYSQL_RES *res = NULL; // mysql 记录集
    if ((res = findsql(sql)) == NULL)
    {
        destroy_xml(root);
        return (-1);
    }

    MYSQL_ROW row;
    while ((row = mysql_fetch_row(res)) != NULL)
    {
        struct xml *item = NULL;
        if ((item = create_xml_and_assigment(row[0], row[1])) != NULL)
        {
            insert_xml_child(root, item);
        }
    }
    memset(data, 0, size);
    print_xml(root, data, size);

    destroy_xml(root);
    mysql_free_result(res); // 释放结果集
    return (-1);
}


int
dao_init_join_page(char *data, int size)
{
    struct xml *root = NULL;
    if ((root = create_xml_root("xml", NULL)) == NULL)
    {
        return (-1);
    }

    /* search */
    char sql[4096];
    snprintf(sql, sizeof(sql), "select * from  init_page where name like 'join_carousel%%'");
	MYSQL_RES *res = NULL; // mysql 记录集
    if ((res = findsql(sql)) == NULL)
    {
        destroy_xml(root);
        return (-1);
    }

    MYSQL_ROW row;
    while ((row = mysql_fetch_row(res)) != NULL)
    {
        struct xml *img = NULL;
        if ((img = create_xml_and_assigment("url", row[1])) != NULL)
        {
            insert_xml_child(root, img);
        }
    }
    memset(data, 0, size);
    print_xml(root, data, size);

    destroy_xml(root);
    mysql_free_result(res); // 释放结果集
    return (-1);
}
